﻿using System;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace CrossEntropyProject
{
    public partial class PieChartForm : Form
    {

        #region constructor
        public PieChartForm()
        {
            InitializeComponent();
            LoadPieChartData();
        }
        #endregion

        #region LoadPieChartData()
        internal void LoadPieChartData()
        {
            int k = Constants.numOfMedoids;
            ArrayList[] clustersArr = PAM.clustersArr;
            ArrayList Medoids = PAM.Medoids;
            ArrayList tempMedoids = new ArrayList();
            int[][] hourclusters = DataSet.hourClusters;

            int[] hours = new int[24];
            int[] hours1 = new int[24];

            for (int i = 0; i < 24; i++)
            {
                hours1[i] = 1;
            }

            chart1.Series["Series1"].Points.DataBindXY(hours, hours1);//create 24 slices in pie chart

            for (int i = 0; i < 24; i++)
            {
                chart1.Series["Series1"].Points[i].BorderWidth = 1;
                chart1.Series["Series1"].Points[i].BorderColor = Color.Black;
                chart1.Series["Series1"].Points[i].Color = Color.White;
                chart1.Series["Series1"].Points[i].Label = i.ToString();
                chart1.Series["Series1"].Points[i].IsVisibleInLegend = false;
            }

            //for (int i = 0; i < Medoids.Count; i++)
            //{
            //    tempMedoids.Add(Medoids[i]);
            //}

            //tempMedoids.Sort();
            //tempMedoids.Reverse();
            //bool flag = false;
            //for (int i = 0, j = k+1 ; i < k; i++, j--) //print on legend the clusters medoids
            //{
            //    if ((int)tempMedoids[i] == 24)
            //    {
            //        j = 1;
            //        chart1.Series["Series1"].Points[0].LegendText = "Cluster " + j.ToString();
            //        flag = true;
            //        j = k+1 ;
            //    }
            //    else
            //    {
            //        if (!flag)
            //        {
            //            j = k;
            //            flag = true;
            //        }
            //        chart1.Series["Series1"].Points[(int)tempMedoids[i]].LegendText = "Cluster " + j.ToString();
            //    }
            //}

            int[] ptr;
            for (int i = 0; i < k; i++)
            {
                //if ((int)Medoids[i] == 24)
                //{
                //   chart1.Series["Series1"].Points[0].IsVisibleInLegend = true;  
                //}
                //else
                //{
                //   chart1.Series["Series1"].Points[(int)Medoids[i]].IsVisibleInLegend = true; 
                //}
                
                ptr = clustersArr[i].Cast<int>().ToArray();

                for (int j = 0; j < ptr.Length; j++)
                {
                    if (ptr[j] == 24)
                    {
                        chart1.Series["Series1"].Points[0].Color = Constants.clArr[i];
                        chart1.Series["Series1"].Points[0].Label = "24";
                    }
                    else
                    {
                        chart1.Series["Series1"].Points[ptr[j]].Color = Constants.clArr[i];// color all the hours of cluster i
                        if ((int)Medoids[i] == 24)
                        {
                            chart1.Series["Series1"].Points[0].Color = Constants.clArr[i];// color the medoid hour too
                            chart1.Series["Series1"].Points[0].Label = "24";
                        }
                        else
                        {
                            chart1.Series["Series1"].Points[(int)Medoids[i]].Color = Constants.clArr[i];// color the medoid hour too 
                        }
                        
                    }//else
                }//for
            }//for

        }//method
        #endregion

        #region checkBox3D_CheckedChanged
        private void checkBox3D_CheckedChanged(object sender, EventArgs e)
        {
            chart1.ChartAreas[0].Area3DStyle.Enable3D = checkBox3D.Checked;
        }
        #endregion

        #region PieChartForm_FormClosing()
        internal void PieChartForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainForm.pieChartShowingFlag = false;
        } 
        #endregion

    }//class
}//namespace
